package com.mobile.check.activitys;

import android.content.Intent;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NLog;

import com.mobile.check.beans.CheckItemInfo;
import com.mobile.check.app.R;
import com.mobile.check.db.EntityManager;
import com.mobile.check.present.BasePresent;

public class ScanResultActivity extends BaseListActivity<CheckItemInfo, BasePresent> {
    String code;

    @Override
    public BaseRecyclerAdapter<CheckItemInfo> getAdapter() {
        return new BaseRecyclerAdapter<CheckItemInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_scanitem;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                CheckItemInfo itemInfo = getItemByPosition(position);
                holder.setText(R.id.itemNameTv, itemInfo.getItemName());
            }
        };
    }


    @Override
    public void initView() {
        super.initView();
        titleTv.setText("检测详情");
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_scanresult;
    }

    @Override
    public void itemClick(int position) {
        Intent intent = new Intent(this, ScanResultDetailActivity.class);
        intent.putExtra("_ID", mBaseAdapter.getItemByPosition(position).get_ID());
        startActivity(intent);
    }

    @Override
    public void getDataFromServer() {
        List<CheckItemInfo> list = EntityManager.getInstance().queryCheckItemByCode(code);
        onSuccess(list);

        if (list.size()>0) {
            String filePath = list.get(0).getPicPath();
            NLog.d(TAG, "filePath=" + filePath);
            File picFile = new File(filePath);
            if (Kits.File.isFileExist(filePath)) {
                Glide.with(this).load(picFile).into((ImageView) findViewById(R.id.picIv));
            } else {
                String picPath = picFile.getParent() + File.separator + code + ".jpeg";
                Glide.with(this).load(picPath).into((ImageView) findViewById(R.id.picIv));
            }
        }

    }

    @Override
    public void getIntentData() {
        code = getIntent().getStringExtra("code");
    }

    @Override
    public BasePresent newP() {
        return new BasePresent();
    }
}
